package com.efast.dmom.mes.datahandlers.entity;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.efast.cafe.framework.util.SpringWebUtils;
import com.efast.dmom.mes.bean.ResultBean;
import com.efast.dmom.mes.bean.Shift;
import com.efast.dmom.mes.bean.ShiftItem;
import com.efast.dmom.mes.dao.entity.ShiftDao;
import com.efast.dmom.mes.datahandlers.base.ConfigurationDataHandler;
import com.efast.dmom.mes.entity.ShiftEntity;
import com.efast.dmom.mes.util.TimeUtil;

@Component
public class ShiftDataHandler extends ConfigurationDataHandler<Shift>{
	@Autowired
	private ShiftDao shiftDao;
	@Autowired
	private ShiftItemDataHandler shiftItemDataHandler;
	
	@Override
	public ResultBean save(Shift shift) {
		ResultBean resultBean = new ResultBean();
		shift.getInnerData().setSiteId(DataHandlerUtils.getSiteId());
		shift.getInnerData().setCreationTime(TimeUtil.getLocalDateTime());
		shift.getInnerData().setCreationTimeZone(TimeUtil.getLocalTimeZone());
		shift.getInnerData().setCreator(SpringWebUtils.getRequestUser());
		shift.getInnerData().setLastModifiedTime(TimeUtil.getLocalDateTime());
		shift.getInnerData().setLastModifiedTimeZone(TimeUtil.getLocalTimeZone());
		shift.getInnerData().setLastModifier(SpringWebUtils.getRequestUser());
//		shift.getInnerData().setCreationPid(dmomIdDao.getDmomPid());
//		shift.getInnerData().setLastModifiedPid(DataHandlerUtils.getDmomPid());
		shift.getInnerData().setRowStatus(1);
		shift.getInnerData().setRowTrxId(1);
		
		shiftDao.save(shift.getInnerData());
		
		return resultBean;
	}
	
	@Override
	public ResultBean update(Shift shift) {
		ResultBean resultBean = new ResultBean();
		
		shift.getInnerData().setLastModifiedTime(TimeUtil.getLocalDateTime());
		shift.getInnerData().setLastModifiedTimeZone(TimeUtil.getLocalTimeZone());
		shift.getInnerData().setLastModifier(SpringWebUtils.getRequestUser());
//		shift.getInnerData().setLastModifiedPid(DataHandlerUtils.getDmomPid());
		shift.getInnerData().setRowTrxId(1);
		
		shiftDao.update(shift.getInnerData());
		return resultBean;
		
	}
	
	@Override
	public ResultBean delete(Shift shift) {
		ResultBean resultBean = new ResultBean();
		shift.getInnerData().setLastModifiedTime(TimeUtil.getLocalDateTime());
		shift.getInnerData().setLastModifiedTimeZone(TimeUtil.getLocalTimeZone());
		shift.getInnerData().setLastModifier(SpringWebUtils.getRequestUser());
//		shift.getInnerData().setLastModifiedPid(DataHandlerUtils.getDmomPid());
		shift.getInnerData().setRowTrxId(1);
		shiftDao.delete(shift.getInnerData());
		return resultBean;
	}
	public Shift getById(long id) {
		Shift shift =  new Shift();
		shift.setInnerData(shiftDao.getById(id));
		
		List<ShiftItem> shiftItemByShiftId = shiftItemDataHandler.getShiftItemByShiftId(id);
		shift.setShiftItems(shiftItemByShiftId);
		return shift;
	}
	public Shift getByName(String name) {
		Shift shift =  new Shift();
		ShiftEntity byName = shiftDao.getByName(name);
		if (byName == null) {
			shift.setInnerData(null);
			return shift;
		}
		shift.setInnerData(byName);
		List<ShiftItem> shiftItemByShiftId = shiftItemDataHandler.getShiftItemByShiftId(byName.getId());
		shift.setShiftItems(shiftItemByShiftId);
		return shift;
	}
	public Shift getByShiftCode(String shiftCode) {
		Shift shift =  new Shift();
		ShiftEntity byShiftCode = shiftDao.getByShiftCode(shiftCode);
		if (byShiftCode == null) {
			shift.setInnerData(null);
			return shift;
		}
		shift.setInnerData(byShiftCode);
		List<ShiftItem> shiftItemByShiftId = shiftItemDataHandler.getShiftItemByShiftId(byShiftCode.getId());
		shift.setShiftItems(shiftItemByShiftId);
		return shift;
	}
	
	public List<Shift> getAll(){
		List<Shift> shifts = new ArrayList<Shift>();
		List<ShiftEntity> shiftEntitys = shiftDao.getAll();
		if(shiftEntitys!=null&&shiftEntitys.size()>0) {
		   for(ShiftEntity shiftEntity:shiftEntitys) {
			   Shift shift = new Shift();
			   shift.setInnerData(shiftEntity);
			   shifts.add(shift);
			   List<ShiftItem> shiftItemByShiftId = shiftItemDataHandler.getShiftItemByShiftId(shiftEntity.getId());
			   shift.setShiftItems(shiftItemByShiftId);
		}
		return shifts;
	}	
	return null;
	}
	public List<Shift> selectShiftByShiftId(List<Long> list){
		List<Shift> shiftList = new ArrayList<Shift>();
		List<ShiftEntity> selectShiftByShiftId = shiftDao.selectShiftByShiftId(list);
		if (selectShiftByShiftId != null && selectShiftByShiftId.size()>0) {
			for (ShiftEntity shiftEntity : selectShiftByShiftId) {
				Shift shift = new Shift();
				   shift.setInnerData(shiftEntity);
				   shiftList.add(shift);
			}
		}
		return shiftList;
	}
	@Override
	public ResultBean saveBatch(List<Shift> obj) {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public ResultBean updateBatch(List<Shift> obj) {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public ResultBean deleteBatch(List<Shift> obj) {
		// TODO Auto-generated method stub
		return null;
	}
}
